// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <gtest/gtest.h>

#include <pollux/expression/simple_function_registry.h>
#include <pollux/expression/vector_function.h>
#include <pollux/functions/prestosql/registration/registration_functions.h>

namespace kumo::pollux::functions::test {

class ScalarFunctionRegTest : public testing::Test {};

TEST_F(ScalarFunctionRegTest, prefix) {
  // Remove all functions and check for no entries.
  exec::vectorFunctionFactories().wlock()->clear();
  exec::mutable_simple_functions().clear_registry();
  EXPECT_EQ(0, exec::vectorFunctionFactories().rlock()->size());
  EXPECT_EQ(0, exec::simple_functions().get_function_names().size());

  // Register without prefix and memorize function maps.
  prestosql::registerAllScalarFunctions();
  const melon::F14FastMap<std::string, exec::VectorFunctionEntry>
      scalarVectorFuncMapBase = *(exec::vectorFunctionFactories().rlock());
  std::unordered_set<std::string> scalarSimpleFuncBaseNames;
  for (const auto& funcName : exec::simple_functions().get_function_names()) {
    scalarSimpleFuncBaseNames.insert(funcName);
  }

  // Remove all functions and check for no entries.
  exec::vectorFunctionFactories().wlock()->clear();
  exec::mutable_simple_functions().clear_registry();
  EXPECT_EQ(0, exec::vectorFunctionFactories().rlock()->size());
  EXPECT_EQ(0, exec::simple_functions().get_function_names().size());

  // Register with prefix and check all functions have the prefix.
  const std::string prefix{"test.abc_schema."};
  const auto prefixSize = prefix.size();
  prestosql::registerAllScalarFunctions(prefix);
  melon::F14FastMap<std::string, exec::VectorFunctionEntry>
      scalarVectorFuncMap = *(exec::vectorFunctionFactories().rlock());

  // Remove special form functions - they don't have any prefix.
  scalarVectorFuncMap.erase("in");
  scalarVectorFuncMap.erase("row_constructor");
  scalarVectorFuncMap.erase("is_null");

  for (const auto& entry : scalarVectorFuncMap) {
    const auto& name = entry.first;
    EXPECT_EQ(prefix, name.substr(0, prefixSize));
    EXPECT_EQ(1, scalarVectorFuncMapBase.count(name.substr(prefixSize)));
  }

  for (const auto& funcName : exec::simple_functions().get_function_names()) {
    // Skip special forms. They don't have any prefix.
    if (funcName == "in") {
      continue;
    }

    // Skip internal functions. They don't have any prefix.
    if (funcName.find("$internal$") == 0) {
      continue;
    }
    EXPECT_EQ(prefix, funcName.substr(0, prefixSize));
    EXPECT_EQ(1, scalarSimpleFuncBaseNames.count(funcName.substr(prefixSize)));
  }
}

} // namespace kumo::pollux::functions::test
